package com.web.portal.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.web.portal.entity.AcademicPrograms;
import com.web.portal.exception.PortalException;
import com.web.portal.service.AcademicProgramsService;

@Controller
public class AcademicProgramsController {
@Autowired
AcademicProgramsService academicProgramsService;

public AcademicProgramsService getAcademicProgramsService() {
	return academicProgramsService;
}

public void setAcademicProgramsService(
		AcademicProgramsService academicProgramsService) {
	this.academicProgramsService = academicProgramsService;
}
@RequestMapping (value="/admin/getacademicprograms.do")
public String getAcademicPrograms (Map<String, Object> model) throws PortalException{
	AcademicPrograms academicPrograms= new AcademicPrograms();
	List<AcademicPrograms> programLists=academicProgramsService.getList(academicPrograms);
	AcademicPrograms a=null;
	if(!programLists.isEmpty()){
		a=programLists.get(0);
	}
	List<AcademicPrograms> programList= new ArrayList<AcademicPrograms>();
	programList.add(a);
	model.put("programList", programList);
	return "academic_programs_page";
}
@RequestMapping (value="/admin/getacademicprogramspage.do", method=RequestMethod.GET)
public String getAddPage(Map model) throws PortalException{
	model.put("academicPrograms",new AcademicPrograms());
	return "add_academic_programs_page";
}
@RequestMapping(value="/admin/addacademicprograms.do", method=RequestMethod.POST)
public String addAcademicPrograms(@Valid @ModelAttribute("academicPrograms")final AcademicPrograms academicPrograms,BindingResult bindingResult, Map model){
	if(!bindingResult.hasErrors()){
		academicProgramsService.saveOrEdit(academicPrograms);
		return "redirect:/admin/getacademicprograms.do";
	}
	return "redirect:/admin/getacademicprograms.do";
}
@RequestMapping (value="/admin/editacademicprograms.do", method=RequestMethod.GET)
public String editAcademicProgramsPage(@RequestParam ("id") final String id, Map model) throws PortalException{
	if(id!=null){
		AcademicPrograms academicPrograms=academicProgramsService.getAcademicPrograms(id);
		model.put("academicPrograms", academicPrograms);
		return "edit_academic_programs_page";
		
	}
	return "redirect:/admin/getacademicprograms.do";
}
@RequestMapping (value="/admin/addeditedacademicprograms.do", method=RequestMethod.POST)
public String addEditedAcademicProgram(@Valid @ModelAttribute("academicPrograms") final AcademicPrograms academicPrograms, BindingResult bindingResult){
	if(!bindingResult.hasErrors()){
		AcademicPrograms thisAcademicPrograms=academicProgramsService.getAcademicPrograms(academicPrograms.getId());
		thisAcademicPrograms.setId(academicPrograms.getId());
		thisAcademicPrograms.setCs(academicPrograms.getCs());
		thisAcademicPrograms.setIs(academicPrograms.getIs());
		thisAcademicPrograms.setIt(academicPrograms.getIt());
		/*
		 * save it
		 */
		academicProgramsService.saveOrEdit(thisAcademicPrograms);
		return "redirect:/admin/getacademicprograms.do";
	}
	return "redirect:/admin/editacademicprograms.do";
}
}
